package com.zoho.daliansheng.utils;

import com.alibaba.fastjson.JSONObject;
import com.zoho.daliansheng.config.DatasourceEnum;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.util.*;


@Slf4j
@UtilityClass
public class U8SystemApiUtil {

    int rows_per_page = 200;

    // 根据公司名称，返回数据源编码
    public String getWebUrlByCompanyName(String companyName) {
        if (companyName.equals(DatasourceEnum.GONG_CHANG.getCompanyName())) {
            return DatasourceEnum.GONG_CHANG.getWebServiceUrl();
        } else if (companyName.equals(DatasourceEnum.SU_ZHOU.getCompanyName())) {
            return DatasourceEnum.SU_ZHOU.getWebServiceUrl();
        } else if (companyName.equals(DatasourceEnum.CHANG_ZHOU.getCompanyName())) {
            return DatasourceEnum.CHANG_ZHOU.getWebServiceUrl();
        }else if (companyName.equals(DatasourceEnum.DA_LIAN.getCompanyName())) {
            return DatasourceEnum.DA_LIAN.getWebServiceUrl();
        }else if (companyName.equals(DatasourceEnum.XIN_CAI_LIAO.getCompanyName())) {
            return DatasourceEnum.XIN_CAI_LIAO.getWebServiceUrl();
        }
        return "";
    }

    // 根据公司名称，返回数据源编码
    public String getCodeByCompanyName(String companyName) {
        if (companyName.equals(DatasourceEnum.GONG_CHANG.getCompanyName())) {
            return DatasourceEnum.GONG_CHANG.getAccountCode();
        } else if (companyName.equals(DatasourceEnum.SU_ZHOU.getCompanyName())) {
            return DatasourceEnum.SU_ZHOU.getAccountCode();
        } else if (companyName.equals(DatasourceEnum.CHANG_ZHOU.getCompanyName())) {
            return DatasourceEnum.CHANG_ZHOU.getAccountCode();
        }else if (companyName.equals(DatasourceEnum.DA_LIAN.getCompanyName())) {
            return DatasourceEnum.DA_LIAN.getAccountCode();
        }else if (companyName.equals(DatasourceEnum.XIN_CAI_LIAO.getCompanyName())) {
            return DatasourceEnum.XIN_CAI_LIAO.getAccountCode();
        }
        return "";
    }

    // 获取交易号
    public JSONObject getTradeId(String url) {
        return HttpUtil.getData(url, new HashMap<>());
    }

    // 获取单条存货档案
    public JSONObject getInventory(String url) {
        return HttpUtil.getData(url, new HashMap<>());
    }

    // 批量获取存货档案
    public List<JSONObject> getBatchInventory(String url) {
        return batchGetData(url, "inventory");
    }

    // 批量获取现存量
    public List<JSONObject> getBatchCurrentstock(String url) {
        return batchGetData(url, "currentstock");
    }

    // 批量获取 收款单
    public List<JSONObject> getBatchAcceptlist(String url) {
        return batchGetData(url, "acceptlist");
    }


    // 新增客户
    public JSONObject addAccount(String url, Map<String, Object> map) {
        return HttpUtil.postData(url, map);
    }

    // 批量获取仓库
    public List<JSONObject> getBatchWarehouse(String url) {
        return batchGetData(url, "warehouse");
    }

    // 批量获取 销售出库单
    public List<JSONObject> getBatchSaleoutlistall(String url) {
        return batchGetData(url, "saleoutlistall");
    }

    // 批量获取人员
    public List<JSONObject> getBatchPerson(String url) {
        return batchGetData(url, "person");
    }

    private List<JSONObject> batchGetData(String url, String field) {
        List<JSONObject> list = new ArrayList<>();
        int page_index = 1;
        boolean flag = true;
        do {
            try {
                JSONObject record = HttpUtil.getData(url + "&page_index=" + page_index + "&rows_per_page=" + rows_per_page, new HashMap<>());
                if (Objects.nonNull(record)) {
                    if (record.getString("errcode").equals("0")) {
                        list.addAll(record.getJSONArray(field).toJavaList(JSONObject.class));
                        page_index++;
                    } else {
                        flag = false;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                flag = false;
            }
        } while (flag);

        return list;
    }
}
